#include<stdio.h>
#include<stdlib.h>
#include<dirent.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>

void remove_dir(const char *path)
{
	DIR *dir = opendir(path);
	if(!dir){	
		perror("opendir");
		return ;
	}

	struct dirent * dp = NULL;
	while(dp = readdir(dir)){
		// . and .. lue guo 
		if(strcmp(dp->d_name,".")==0||strcmp(dp->d_name,"..")==0)
			continue;

		char buf[256] = {};
		sprintf(buf,"%s/%s",path,dp->d_name);	
		
		//dir
		if(dp->d_type==DT_DIR){
			remove_dir(buf);
		}
		else{		

			unlink(buf);
		}
	}
	

	closedir(dir);
	
	//delete dir
	rmdir(path);

}



int main(int argc,char *argv[])
{
	if(argc!=2){
		printf("%s dirpath\n",argv[0]);
		return -1;
	}
	
	remove_dir(argv[1]);
	
	return 0;


}
